• Workflows
  • Writing measurement reports with Python and Zettlr

Originally, I installed Zettlr to use it as a personal knowledge management system – writing my daily notes, keeping track of tasks and having one place for all my information about customers, products, systems, standards and such. When I discovered how powerful Zettlr is in combination with Pandoc and LaTeX, I integrated it into my workflow of writing my measurement reports as well.

I use Python to analyse the data I measured. My Python code also generates the graphs and tables of the results and moves them into a specific folder. I have to include all of it in the appendix and it’s rather boring to manually generate image captions, so I let Python do that – it „knows“ what they’re about anyway! For example, I have something like this:

report = ' \n'.join([
  f'# Measurement of {system} {serial number} on {date}',
  '## Results', 
  f'{results.loc[:, ['x', 'y', 'z']].to_markdown()}',
  r'\clearpage',
])

for x in y
	report = report + f'\n![y for {x}](<Result_y_{x}.png>) \n'

for x in z
	report = report + f'\n![z for {x}](<Result_z_{x}.png>) \n'  

with open(f'report for {system} {model} {serial number} {date}.md, mode='w', encoding='utf-8') as report_file:
	report_file.write(report)

This generates a Markdown file with all the graphs with captions and also a table with my data.

Of course, I still have to write my actual report and findings. Now I can use Zettlr to edit this pre-generated Markdown file and write my assessment of the measurement.

With the Markdown file done, I use Zettlr to export my report as a PDF. I don’t even need to define frontmatter for this since it’s the same for every report. So I set up a custom export profile in Zettlr:

  • I write my Markdown files with the h1-headline as title, so I use shift-heading-level-by: -1 to give the first headline as the title variable to LaTeX and shift all other headings one level up
  • I’d like to have a title page, so I define titlepage: true under variables
  • There are some logos and headers I need to include. Under variables, I define titlepage-logo: 'logo_titlepage.png', header-right: '\includegraphics[width=100pt]{logo_header.png}' (the template also allows text as header-right, so I need to include a LaTeX command here)
  • I don’t want a written Table of Contents – the PDF TOC is just fine. toc: false
  • My reports are in German, so I want the captions etc. to be localised. Under variables, I set lang: de-DE.
  • The report is mostly read on 16:9 screens and includes a lot of graphs which are in landscape as well. So I set geometry: landscape under variables
  • I have a LaTeX template, so I put in template: measurement-report

Now I just need to Export the file via Zettlr, choose my template and can send a finished report while keeping the Markdown file in my Zettlr folder so I can include it in my future work.

Let me know if that was helpful to you or if you have tips to improve this!

I love how much you've automated this process, great work!

That sounds very nice! One thing I'm wondering: If your workflow is to write your written report into the generated Markdown, does that mean that you cannot change anything about your data analysis after you've begun writing? Since then you'd have to re-generate the Markdown? Are you just very disciplined about finishing the analysis before drafting anything, or do you have some way to do both in parallel?

    gbm Good point! I keep a separate file for notes on the whole measurement which I also use to write down bullet points about the evaluation – e.g. hypotheses I need to verify or measurement data that needs explanation – so I never start writing any of the long-form text that goes into the report in the end before I generated the markdown file, and I usually don't revisit the whole Python process once I'm finished. So far, all the errors were caught before I wrote the report... 😃

    I thought about using multiple Markdown files before, but did not put that plan into action yet. Zettlr's support of projects should be a great fit for this, though. I will update the post once I improved this process. Thanks for the nudge!

    Ah that makes sense, I thought you might have separate notes for the prose.

    I was asking because I often had similar problems when working with paper drafts and a markdown -> docx workflow. However that was because I never quite managed to get the export pipeline to produce exactly what I needed for presentation, so I often ended up repeating the same (small) edits after every revision.

    In any case, your setup sounds great, I hope you have fun tinkering with it more 🙂